package com.situ.day25.consumerproducer;

import java.util.LinkedList;

// wait、notifyAll是Object类里面的方法
public class Panzi extends Object{
    private LinkedList<Cake> list = new LinkedList<>();

    //java.lang.IllegalMonitorStateException
    //生产者线程调用
    public synchronized void putCake(Cake cake) {
        //往尾部添加
        list.addLast(cake);
        notifyAll();
        System.out.println("生产者线程 putCake notifyAll");
    }

    //消费者线程调用
    public synchronized Cake getCake() {
        if (list.size() <= 0) {//盘子里面没有蛋糕,消费者需要等待
            try {
                System.out.println("消费者线程 getCake wait");
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        //头部移除
        Cake cake = list.removeFirst();
        return cake;
    }
}
